"""
Case Type   : cm_ctl
Case Name   : 参数event_triggers值不包含特定值/特定值重复,合理报错
Create At   : 2023/04/24
Owner       : opentestcase012
Description :
    1.修改参数值event_triggers='{"":""}';
    2.修改参数值event_triggers不是string类型;
    3.修改参数值event_triggers不是shell脚本;
    4.修改参数event_triggers文件未未指定路径;
    5.修改参数event_triggers文件不存在;
    6.修改参数event_triggers异常;
    7.修改参数event_triggers值包含空格;
Expect      :
    1.失败，The trigger value can't be empty.
    2.失败，The trigger value must be string.
    3.失败，The trigger value /data/xxx/cm/2 is not shell script.
    4.失败，The trigger script path must be absolute path.
    5.失败，The trigger script /data/xxx/cm/2.sh is not a file or does not exist
    6.失败，The value of event_triggers must be an object.
    7.失败，The parameter value({"on_start": "/data/xxx/cm/on_start.sh"}) exists illegal character:" ".
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != primary_sh.get_node_num(), '非1+2环境不执行')
class DefinedResource(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.common = Common()
        self.pri_user = Node('PrimaryDbUser')
        self.primary_sh = primary_sh
        self.file1 = os.path.join(macro.DB_MY_PATH, 'event_triggers')
        self.file2 = os.path.join(macro.DB_MY_PATH, 'event_triggers1.sh')
        self.file3 = os.path.join(macro.DB_MY_PATH, 'event_triggers.sh')

    def test_cm(self):
        text = '----在所有节点创建文件并写入内容,赋给文件执行权限; expect:成功----'
        self.log.info(text)
        cmd = f"echo 'echo event_triggers_sh contents' > {self.file1};" \
            f"echo 'echo event_triggers_sh contents' > {self.file3};" \
            f"chmod +x {self.file1} {self.file3};"
        res = self.primary_sh.exec_gs_ssh(cmd)
        self.assertIn('Successfully execute command on all nodes', res,
                      "执行失败:" + text)

        text = '----step1:修改参数值event_triggers=\'{"":""}\'; expect:修改失败----'
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
            f"cm_ctl set --param --agent -k " \
            f"event_triggers='{{{{\"\":\"\"}}}}';"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.pri_user, cmd)
        self.assertIn("The trigger value can't be empty", res,
                      "执行失败:" + text)

        text = '----step2:修改参数值event_triggers不是string类型; expect:修改失败----'
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
            f"cm_ctl set --param --agent -k " \
            f"event_triggers='{{{{\"a\":[{{{{\"1\":\"2\",\"3\":\"4\"}}}}]}}}}';"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.pri_user, cmd)
        self.assertIn("The trigger value must be string", res,
                      "执行失败:" + text)

        text = '----step3:修改参数值event_triggers不是shell脚本; expect:修改失败----'
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
            f"cm_ctl set --param --agent -k " \
            f"event_triggers='{{{{\"1\":\"{self.file1}\"}}}}';"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.pri_user, cmd)
        self.assertIn("not shell script", res, "执行失败:" + text)

        text = '----step4:修改参数event_triggers文件未未指定路径; expect:修改失败----'
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
            f"cm_ctl set --param --agent -k " \
            f"event_triggers='{{{{\"1\":\"2\"}}}}';"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.pri_user, cmd)
        self.assertIn("The trigger script path must be absolute path", res,
                      "执行失败:" + text)

        text = '----step5:修改参数event_triggers文件不存在; expect:修改失败----'
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
            f"cm_ctl set --param --agent -k " \
            f"event_triggers='{{{{\"1\":\"{self.file2}\"}}}}';"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.pri_user, cmd)
        self.assertIn("not a file or does not exist", res, "执行失败:" + text)

        text = '----step6:修改参数event_triggers异常; expect:修改失败----'
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
            f"cm_ctl set --param --agent -k " \
            f"event_triggers='1';"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.pri_user, cmd)
        self.assertIn("The value of event_triggers must be an object", res,
                      "执行失败:" + text)

        text = '----step7:修改参数event_triggers值包含空格; expect:修改失败----'
        self.log.info(text)
        cmd = f"source {macro.DB_ENV_PATH};" \
            f"cm_ctl set --param --agent -k " \
            f"event_triggers='{{{{\"start\": \"{self.file3}\"}}}}';"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.pri_user, cmd)
        self.assertIn('exists illegal character:" "', res, "执行失败:" + text)

    def tearDown(self):
        self.log.info('-----清理环境-----')
        text1 = '----step8:恢复参数值并执行reload生效; expect:成功----'
        self.log.info(text1)
        cmd = f"source {macro.DB_ENV_PATH};" \
            "cm_ctl set --param --agent -k event_triggers='{{}}';" \
            f"cm_ctl reload --param --agent"
        self.log.info(cmd)
        cm_ctl_res1 = self.common.get_sh_result(self.pri_user, cmd)

        text2 = '----删除sh文件; expect:成功----'
        self.log.info(text2)
        cm_ctl_res2 = self.primary_sh.exec_gs_ssh(
            f'rm -rf {self.file1} {self.file2} {self.file3}')

        self.assertEqual(2, cm_ctl_res1.count('success'),
                         "执行失败:" + cm_ctl_res1)
        self.assertIn('Successfully execute command on all nodes', cm_ctl_res2,
                      "执行失败:" + text2)
        self.log.info(f'----{os.path.basename(__file__)} finish----')
